/*
 * 项目基本的JS框架
 * 提供了对Ajax和选择器的支持
 * @param {Object} w
 * @memberOf {TypeName} 
 * @return {TypeName} 
 */
(function(w){
var 
	window = w,
	undefined,
	javer = $ = window.javer = window.$ = function(selector) {
		return new javer.fn.init(selector);
	};
	
	// Javer Framework Constructor
	
	javer.fn = javer.prototype = {
		init: function(selector) {
			selector = selector || document;
			if (selector.nodeType) {
				this[0] = selector;
			}
			else {
				var domobj = document.getElementById(selector);
				this[0] = domobj;
			}
			return this;
		},
		get: function(i) {
			i = i || 0;
			return this[i];
		}
	}
	
	javer.fn.init.prototype = javer.fn;
	
	javer.extend = javer.fn.extend = function(obj, prop) {
		if (!prop) {
			prop = obj;
			obj = this;
		}
		for (var p in prop) {
			obj[p] = prop[p];
		}
	}
	
	javer.extend({
		get: function(url, callback) {
			var xhr = window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
			xhr.open("GET", url, true);
			xhr.onreadystatechange = function() {
				if (xhr.readyState != 4) {
					return;
				}
				if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
					//javer.log("responseText is " + res);
					if (typeof callback == 'function') {
						var res = xhr.responseText;
						callback(res);
					}
				}
				else {
					javer.log("error occurs, xhr.status = " + xhr.status);
					// error occurs
				}
				// Stop memory leaks
				xhr.onreadystatechange = function(){};
			}
			xhr.send();
		},
		getJSON: function(url, callback) {
			javer.get(url, function(res) {
				callback(eval("("+ res +")"));
			})
		},
		trim: function(string) {
			var tmp = string || "";
			tmp = tmp.replace(/^\s+|\s+$/g, "");
			return tmp;
		}
	});
	
	javer.fn.extend({
		html: function(html) {
			if (arguments.length == 1) {
				this[0].innerHTML = html || "";
			}
			else {
				return this[0].innerHTML;
			}
		},
		value: function(v) {
			if (arguments.length == 1) {
				this[0].value = v;
			}
			else {
				return this[0].value;
			}
		},
		load: function(url) {
			var me = this;
			javer.get(url, function(res) {
				me.html(res); 
			});
			
		},
		style: function(classname) {
			if (!classname) {
				return this[0].className;
			}
			else {
				this[0].className = classname;
			}
		},
		css: function(name, val) {
			if (val) {
				this[0].style[name] = val;
				return this;
			}
			else {
				return this[0].style[name];
			}
		},
		attr: function(name, val) {
			if (val) {
				(this[0])[name] = val;
				return this;
			}
			else {
				return (this[0])[name];
			}
		},
		hide: function() {
			this[0].style.display = "none";
		},
		display: function() {
			this[0].style.display = "block";
		}
	});
	
	javer.extend({
		log: function(message) {
			try {
				console.log(message || "");
			}
			catch(e) {
				// do nothing.
			}
		},
		configurError: function() {
			window.onerror = function(message, url, line) {
				window.status = message;
				javer.log("Errors occurs: " + message + "#" + url + "#" + line);
				return true;
			}
		}
	});
	
	javer.Cookies = {
	    set : function(name, value){
	        var argv = arguments;
	        var argc = arguments.length;
	        var expires = (argc > 2) ? argv[2] : null;
	        var path = (argc > 3) ? argv[3] : '/';
	        var domain = (argc > 4) ? argv[4] : null;
	        var secure = (argc > 5) ? argv[5] : false;
	        document.cookie = name + "=" + escape(value) + ((expires === null) ? "" : ("; expires=" + expires.toGMTString())) + ((path === null) ? "" : ("; path=" + path)) + ((domain === null) ? "" : ("; domain=" + domain)) + ((secure === true) ? "; secure" : "");
	    },
	
	    get : function(name){
	        var arg = name + "=";
	        var alen = arg.length;
	        var clen = document.cookie.length;
	        var i = 0;
	        var j = 0;
	        while(i < clen){
	            j = i + alen;
	            if(document.cookie.substring(i, j) == arg){
	                return $.Cookies.getCookieVal(j);
	            }
	            i = document.cookie.indexOf(" ", i) + 1;
	            if(i === 0){
	                break;
	            }
	        }
	        return null;
	    },

	    clear : function(name){
	        if($.Cookies.get(name)){
	            document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	        }
	    },

	    getCookieVal : function(offset){
	        var endstr = document.cookie.indexOf(";", offset);
	        if(endstr == -1){
	            endstr = document.cookie.length;
	        }
	        return unescape(document.cookie.substring(offset, endstr));
	    }
	};
	
})(window);

